﻿Imports System.Data.SqlClient

Public Class CMatHang
    Dim _strMaMH As String
    Dim _strTenMH As String
    Dim _fGia As String
    Dim _iSoLuong As String
    Dim _strLoai As String
    Dim _strDonVi As String
    Dim _strXuatXu As String
    Shared da As New DataProvider
    Property XuatXu As String
        Get
            Return _strXuatXu
        End Get
        Set(ByVal value As String)
            _strXuatXu = value
        End Set
    End Property
    Property MaMH As String
        Get
            Return _strMaMH
        End Get
        Set(ByVal value As String)
            _strMaMH = value
        End Set
    End Property
    Property TenMH As String
        Get
            Return _strTenMH
        End Get
        Set(ByVal value As String)
            _strTenMH = value
        End Set
    End Property
    Property Gia As String
        Get
            Return _fGia
        End Get
        Set(ByVal value As String)
            _fGia = value
        End Set
    End Property
    Property SoLuong As String
        Get
            Return _iSoLuong
        End Get
        Set(ByVal value As String)
            _iSoLuong = value
        End Set
    End Property
    Property Loai As String
        Get
            Return _strLoai
        End Get
        Set(ByVal value As String)
            _strLoai = value
        End Set
    End Property
    Property DonVi As String
        Get
            Return _strDonVi
        End Get
        Set(ByVal value As String)
            _strDonVi = value
        End Set
    End Property

    Public Shared Function LoadMatHang() As DataTable
        da = New DataProvider()
        Dim dt As New DataTable
        da.OpenConnect()
        Dim sql As String = "Select * from SANPHAM"
        dt = da.ShowData(sql)
        da.CloseConnect()
        Return dt
    End Function
    Public Shared Sub Insert(ByVal mh As CMatHang)
        Try
            da.OpenConnect()
            Dim sql = "INSERT INTO SANPHAM VALUES(@TenMH,@Loai,@Gia,@XuatXu,@DonVi,@SoLuong)"
            Dim cmd = New SqlCommand(sql, da.con)
            cmd.Parameters.AddWithValue("@TenMH", mh.TenMH)
            cmd.Parameters.AddWithValue("@Gia", mh.Gia)
            cmd.Parameters.AddWithValue("@Loai", mh.Loai)
            cmd.Parameters.AddWithValue("@XuatXu", mh.XuatXu)
            cmd.Parameters.AddWithValue("@DonVi", mh.DonVi)
            cmd.Parameters.AddWithValue("@SoLuong", mh.SoLuong)
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Thông báo")
        Finally
            da.CloseConnect()
        End Try
    End Sub
    Public Shared Sub Delete(ByVal MaSanPham As String)
        Try
            da.OpenConnect()
            Dim Sql = "DELETE FROM SANPHAM WHERE MaSanPham = '" + MaSanPham + "'"
            da.OpenConnect()
            Dim cmd As New SqlCommand(Sql, da.con)
            cmd.ExecuteNonQuery()
            MsgBox("Xóa Thành công", MsgBoxStyle.Information, "Thông báo")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Thông báo")
        End Try
    End Sub
    Public Shared Sub Update(ByVal mh As CMatHang)
        Try
            da = New DataProvider()
            da.OpenConnect()
            Dim sql = ""
            sql = "UPDATE MATHANG " + _
                                "SET TenMH = @TenMH, Gia = @Gia,Loai = @Loai,DonVi = @DonVi,SoLuong = @SoLuong)" + _
                                "WHERE MaMH = @MaMH)"
            Dim cmd As New SqlCommand(sql, da.con)
            cmd.Parameters.AddWithValue("@MaMH", mh.MaMH)
            cmd.Parameters.AddWithValue("@TenMH", mh.TenMH)
            cmd.Parameters.AddWithValue("@Gia", mh.Gia)
            cmd.Parameters.AddWithValue("@Loai", mh.Loai)
            cmd.Parameters.AddWithValue("@DonVi", mh.DonVi)
            cmd.Parameters.AddWithValue("@SoLuong", mh.SoLuong)
            cmd.ExecuteNonQuery()
            MsgBox("Update Thành công", MsgBoxStyle.Information, "Thông báo")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Thông báo")
        Finally
            da.CloseConnect()
        End Try
    End Sub
    Public Shared Function GetData(ByVal sql As String) As Object
        da = New DataProvider
        da.OpenConnect()
        Dim obj As Object = da.GetData(sql)
        da.CloseConnect()
        Return obj
    End Function
End Class
